

var async 		= require('async')
,	fs 			= require('fs')
,	jsp 		= require("uglify-js").parser
,	pro 		= require("uglify-js").uglify;


exports.build = function () {

	var sauberfant	= this
	,	start		= new Date().getTime();

	for (var i = sauberfant.options.build.length - 1; i >= 0; i--) {

		var file	= sauberfant.options.build[i].file,
			files	= sauberfant.options.build[i].files;

		(function(file, files){

			async.map(files, fs.readFile, function (err, results) {

				var code	 	= results.join("\n");
				//,	ast 		= jsp.parse(code) 	// parse code and get the initial AST
				//,	ast			= pro.ast_mangle(ast) 	// get a new AST with mangled names
				//,	ast 		= pro.ast_squeeze(ast) 	// get an AST with compression optimizations
				//,	code		= pro.gen_code(ast); 	// compressed code here

				fs.writeFile(sauberfant.options.dirs.public  + file + '.js', code, 'utf-8', function () {
					var duration = new Date().getTime() - start;
					console.log("Finished compiling to " + file + ".js (" + duration + "ms)");
				});
			});

		})(file, files);

	};

}

